//
//  LBNetworkReachability.m
//  LBNetworking
//
//  Created by WuShiHai on 19/5/30.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBNetworkReachability.h"

#import <AFNetworking/AFNetworking.h>

NSString * const LBNetworkReachabilityChageNotification = @"LBNetworkReachabilityChageNotification";

#define WL_IsStrEmpty(_ref)    (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref)isEqualToString:@""]))

static AFHTTPSessionManager *http_request_operation_manager(NSString *checkURLString){
    static AFHTTPSessionManager *wl_http_request_operation_manager;
    static dispatch_once_t oneToken;
    if (WL_IsStrEmpty(checkURLString)) {
        checkURLString = @"http://www.baidu.com";
    }
    dispatch_once(&oneToken,^{
        NSURL *baseURL = [NSURL URLWithString:checkURLString];
        wl_http_request_operation_manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    });
    return wl_http_request_operation_manager;
}

AFNetworkReachabilityStatus netwokStatus = AFNetworkReachabilityStatusReachableViaWWAN;//默认环境
BOOL monitorIsInit;

@implementation LBNetworkReachability

+ (void)startMonitor:(NSString *)checkURLString{
    [LBNetworkReachability startMonitor:checkURLString
                                viaWWAN:nil
                                viaWiFi:nil
                           notReachable:nil];
}

+ (void)startMonitor:(NSString *)checkURLString
             viaWWAN:(LBNetworkReachabilityBlock)viaWWANBlock
             viaWiFi:(LBNetworkReachabilityBlock)viaWiFiBlock
        notReachable:(LBNetworkReachabilityBlock)notReachableBlock{
    
    AFHTTPSessionManager *manager = http_request_operation_manager(checkURLString);
    monitorIsInit = YES;
    
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                netwokStatus = AFNetworkReachabilityStatusReachableViaWWAN;
                viaWWANBlock?viaWWANBlock():nil;
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                netwokStatus = AFNetworkReachabilityStatusReachableViaWiFi;
                viaWiFiBlock?viaWiFiBlock():nil;
            }
                break;
            case AFNetworkReachabilityStatusNotReachable:{
                netwokStatus = AFNetworkReachabilityStatusNotReachable;
                notReachableBlock?notReachableBlock():nil;
            }
                break;
            default:
                break;
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:LBNetworkReachabilityChageNotification object:@(status)];
    }];
    
    [manager.reachabilityManager startMonitoring];
}

+ (AFNetworkReachabilityStatus)status{
    return netwokStatus;
}
+ (BOOL)isReachable{
    if (!monitorIsInit) {//如果没有初始化则默认可通
        return YES;
    }
    if (netwokStatus == AFNetworkReachabilityStatusReachableViaWWAN || netwokStatus == AFNetworkReachabilityStatusReachableViaWiFi) {
        return YES;
    }else{
        return NO;
    }
}

@end
